package com.ming.yourdiary;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.ming.yourdiary.dataMannager.DiaryDBOpenHelper;
import com.ming.yourdiary.common.AppConstants;
import com.qweather.sdk.view.HeConfig;

public class YourDiaryApplication extends Application {

  private static final String TAG = "YourDiaryApplication";
  private static final String DATABASE_NAME = "YourDiary.db";

  public static YourDiaryApplication sApp;
  public static Context sContext;

  public static String sUserLocation;
  public static SQLiteDatabase sDiaryNoteDateBase;

  @Override
  public void onCreate() {
    super.onCreate();
    sApp = this;
    sContext = this;

    initAppSDKs();
    initDB();
  }




  //初始化SDK
  private void initAppSDKs() {
    initUserLocation();
    initWeatherSDK();
  }

  //初始化用户所在地址
  private void initUserLocation(){
    //TODO 暂时固定为青岛-崂山区 后续更改为GeoAPI获取
    sUserLocation = "120.51,36.19";
    //sUserLocation = UiTools.getUserLocationByGPS();
    Log.d(TAG,"initUserLocation - "+sUserLocation);
  }

  //初始化和风天气SDK
  private void initWeatherSDK() {
    HeConfig.init(AppConstants.WEATHER_SDK_PUBLIC_ID, AppConstants.WEATHER_SDK_KEY);
    if ("DEV".equals(AppConstants.WEATHER_SDK_VERSION)) {
      //切换至开发版服务
      HeConfig.switchToDevService();
    }else {
      //切换至商业版服务
      HeConfig.switchToBizService();
    }
  }

  //初始化SQLLite数据库
  private void initDB(){
    DiaryDBOpenHelper dbOpenHelper = new DiaryDBOpenHelper(sApp,DATABASE_NAME);
    sDiaryNoteDateBase = dbOpenHelper.getWritableDatabase();
  }

  //关闭数据库链接
  private void closeDB(){
    if (sDiaryNoteDateBase != null) {
      sDiaryNoteDateBase.close();
    }
  }

  @Override
  public void onTerminate() {
    closeDB();
    super.onTerminate();
  }
}
